package com.myspring.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * ClassName: ServiceAdvice
 * Package: com.myspring.advice
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/2/4 22:13
 * @Version 1.0
 */

@Component
public class ServiceAdvice {
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println(Arrays.toString(joinPoint.getArgs()));
        System.out.println("前置通知");
    }

    public void afterReturningAdvice(JoinPoint joinPoint , Object returnValue) {
        System.out.println("获取到的返回值:"+returnValue);
        System.out.println("后置通知");
    }

    public void finallyAdvice(JoinPoint joinPoint) {
        System.out.println("最终通知");
    }

    public void afterThrowingAdvice(JoinPoint joinPoint,Throwable throwable) {
        System.out.println(throwable.getMessage()+"------");
        System.out.println("异常通知");
    }

    public void aroundAdvice(ProceedingJoinPoint pjp) {
        try {
            System.out.println("前置通知");
            Object returnVal = pjp.proceed();//执行目标方法
            System.out.println(returnVal);
            System.out.println("后置通知");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("异常通知");
        } finally {
            System.out.println("最终通知");
        }
    }
}
